package com.example.bingobox;

import com.example.bingobox.service.MyAccessibilityService;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class PermissionGuideActivity extends AppCompatActivity {
    private Button btnAccessibilityPermission;
    private TextView tvAccessibilityStatus;
    private Button btnContinue;
    private Button btnEnter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission_guide);

        btnAccessibilityPermission = findViewById(R.id.btnAccessibilityPermission);
        tvAccessibilityStatus = findViewById(R.id.tvAccessibilityStatus);
        btnContinue = findViewById(R.id.btnContinue);
        btnEnter = findViewById(R.id.btnEnter);

        updatePermissionStatus();

        btnAccessibilityPermission.setOnClickListener(v -> {
            Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
            startActivity(intent);
        });

        btnContinue.setOnClickListener(v -> {
            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            Uri uri = Uri.fromParts("package", getPackageName(), null);
            intent.setData(uri);
            startActivity(intent);
        });

        btnEnter.setOnClickListener(v -> {
            if (isAccessibilityServiceEnabled()) {
                startMainActivity();
            } else {
                // 提示用户需要授予所有权限
                android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
                builder.setTitle("提示")
                       .setMessage("某些权限未授予，应用可能无法正常工作。是否继续？")
                       .setPositiveButton("继续", (dialog, which) -> startMainActivity())
                       .setNegativeButton("返回", null)
                       .show();
            }
        });
    }

    private void updatePermissionStatus() {
        boolean hasAccessibility = isAccessibilityServiceEnabled();
        tvAccessibilityStatus.setText(hasAccessibility ? "已授权" : "未授权");
        tvAccessibilityStatus.setTextColor(hasAccessibility ? 0xFF00FF00 : 0xFFFF0000);
    }

    private boolean isAccessibilityServiceEnabled() {
        int accessibilityEnabled = 0;
        try {
            accessibilityEnabled = Settings.Secure.getInt(
                    getContentResolver(),
                    Settings.Secure.ACCESSIBILITY_ENABLED
            );
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }

        if (accessibilityEnabled == 1) {
            String service = getPackageName() + "/" + MyAccessibilityService.class.getCanonicalName();
            String enabledServices = Settings.Secure.getString(
                    getContentResolver(),
                    Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES
            );
            return enabledServices != null && enabledServices.contains(service);
        }
        return false;
    }

    @Override
    protected void onResume() {
        super.onResume();
        updatePermissionStatus();
    }

    private void startMainActivity() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
} 